package com.aiti.lulian.service.impl;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardEnterpriseCircleApplicationDto;
import com.aiti.lulian.entity.CardEnterpriseCircle;
import com.aiti.lulian.entity.CardEnterpriseCircleApplication;
import com.aiti.lulian.entity.CardEnterpriseCircleUserCard;
import com.aiti.lulian.entity.newCard.CardBaseInfo;
import com.aiti.lulian.entity.newCard.CardMessage;
import com.aiti.lulian.mapper.CardEnterpriseCircleApplicationMapper;
import com.aiti.lulian.mapper.CardEnterpriseCircleMapper;
import com.aiti.lulian.mapper.CardEnterpriseCircleUserCardMapper;
import com.aiti.lulian.mapper.newCard.CardMessageMapper;
import com.aiti.lulian.mapper.newCard.CardPersonalBaseInfoMapper;
import com.aiti.lulian.service.CardEnterpriseCircleApplicationService;
import com.aiti.lulian.vo.CardEnterpriseCircleApplicationVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CardEnterpriseCircleApplicationServiceImpl extends BaseSerivceImpl<CardEnterpriseCircleApplicationMapper, CardEnterpriseCircleApplication> implements CardEnterpriseCircleApplicationService {

    @Autowired
    private CardEnterpriseCircleApplicationMapper cardEnterpriseCircleApplicationMapper;
    @Autowired
    private CardEnterpriseCircleUserCardMapper cardEnterpriseCircleUserCardMapper;
    @Autowired
    private CardEnterpriseCircleMapper cardEnterpriseCircleMapper;
    @Autowired
    private CardMessageMapper cardMessageMapper;
    @Autowired
    private CardPersonalBaseInfoMapper cardPersonalBaseInfoMapper;

    @Override
    public List<CardEnterpriseCircleApplicationVo> getApplicationList(CardEnterpriseCircleApplicationDto dto, Page<CardEnterpriseCircleApplication> page) {
        return cardEnterpriseCircleApplicationMapper.selectApplicationList(dto, page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Message approveApplication(CardEnterpriseCircleApplicationDto dto) {
        Integer status = dto.getStatus();
        CardEnterpriseCircleApplication circleApplication = this.getById(dto.getBaseId());
        circleApplication.setStatus(status);
        circleApplication.setApproverCardId(dto.getApproverCardId());
        circleApplication.setApproveTime(new Date());
        this.updateById(circleApplication);

        CardEnterpriseCircle cardEnterpriseCircle = cardEnterpriseCircleMapper.selectById(circleApplication.getCircleId());
        CardMessage cardMessage;
        if (status == 1) {
            // 同意
            // 加到圈子成员表中
            CardEnterpriseCircleUserCard cardEnterpriseCircleUserCard = new CardEnterpriseCircleUserCard()
                    .setCircleId(circleApplication.getCircleId())
                    .setInviterCardId(circleApplication.getInviterCardId())
                    .setCardId(circleApplication.getApplicantCardId())
                    .setLabelId(circleApplication.getLabelId())
                    .setType("0")
                    .setBaseCreateTime(new Date())
                    .setBaseUpdateTime(new Date());
            cardEnterpriseCircleUserCardMapper.insert(cardEnterpriseCircleUserCard);
            // 更新圈子表中的人数字段
            cardEnterpriseCircle.setJoinCount(cardEnterpriseCircle.getJoinCount() + 1);
            cardEnterpriseCircleMapper.updateById(cardEnterpriseCircle);
            // 向申请人发送同意加入圈子的消息
            CardBaseInfo cardBaseInfo = cardPersonalBaseInfoMapper.selectById(dto.getApproverCardId());
            String username = cardPersonalBaseInfoMapper.selectUsernameByUserId(cardBaseInfo.getUserId());
            cardMessage = CardMessage.builder().source(3).senderCardId(dto.getApproverCardId()).receiverCardId(circleApplication.getApplicantCardId()).content(username + (StrUtil.isNotEmpty(cardBaseInfo.getName()) ? ("（" + cardBaseInfo.getName() + "）") : "") + "同意您加入圈子：" + cardEnterpriseCircle.getBaseName()).baseCreateTime(new Date()).build();
        } else {
            // 拒绝
            Integer rejectType = dto.getRejectType();
            if (rejectType == 0) {
                // 忽略该申请，不向对方发送拒绝通知
                return Message.success();
            } else {
                // 发送以“名片信息填写不规范”或“不符合加入条件”为原因的拒绝通知
                CardBaseInfo cardBaseInfo = cardPersonalBaseInfoMapper.selectById(dto.getApproverCardId());
                String username = cardPersonalBaseInfoMapper.selectUsernameByUserId(cardBaseInfo.getUserId());
                cardMessage = CardMessage.builder().source(3).senderCardId(dto.getApproverCardId()).receiverCardId(circleApplication.getApplicantCardId()).content(username + (StrUtil.isNotEmpty(cardBaseInfo.getName()) ? ("（" + cardBaseInfo.getName() + "）") : "") + "拒绝您加入圈子：" + cardEnterpriseCircle.getBaseName() + "，原因是：" + (rejectType == 1 ? "名片信息填写不规范" : "不符合加入条件")).baseCreateTime(new Date()).build();
            }
        }
        cardMessageMapper.insert(cardMessage);
        return Message.success();
    }
}
